$(function(){
    var admin_id = getCookie('admin_id');
    if(!admin_id){
        window.location.replace('login.html');
    }
    var elem = document.querySelector('.js-switch');
    var switchery = new Switchery(elem, {
        color: '#1AB394'
    });


    //获取列表
    $.ajax({
        url: ApiUrl + "/taocang/type",
        type: 'get',
        dataType: 'JSON',
        success: function(result) {

            if(result.code == 200){
                var html='<option value="0">---请选择---</option>';
                $.each(result.data, function(k, v) {
                    html+='<option value="'+v.taocang_type_id+'">'+v.taocang_type_name+'</option>';
                });
                $("#taocang_type_id").html(html);
            }else{
                parent.layer.msg(result.message);
                return false;
            }
        }
    });

    //搜索商品
    $('#btn_goods_name').on('click', function() {
        var goods_name = $('#search_goods_name').val();
        //获取列表
        $.ajax({
            url: ApiUrl + "/taocang/goods?goods_name="+goods_name,
            type: 'get',
            dataType: 'JSON',
            success: function(result) {
                if(result.code == 200){
                    var html='';
                    $.each(result.data, function(k, v) {
                        html+='<label class="checkbox-inline"><input type="checkbox" name="goods_arr[]" id="goods_'+v.id+'" value="'+v.id+'">'+v.zh_name+'</label>';
                    });
                    $("#serch_goods").append(html);
                }else{
                    parent.layer.msg(result.message);
                    return false;
                }
            }
        });
    });
    //自定义radio样式
    $(".cb-enable").click(function(){
        var parent = $(this).parents('.onoff');
        $('.cb-disable',parent).removeClass('selected');
        $(this).addClass('selected');
        $('.checkbox',parent).attr('checked', true);
    });
    $(".cb-disable").click(function(){
        var parent = $(this).parents('.onoff');
        $('.cb-enable',parent).removeClass('selected');
        $(this).addClass('selected');
        $('.checkbox',parent).attr('checked', false);
    });
    //提交表单
    $("#submit").click(function(){
        if($("#signupForm").valid()){
            var query = $("#signupForm").serialize();
            ajax_post(ApiUrl + "/taocang/created","taocang_list.html",query);
        }
    });
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            title: {
                required: true
            },
            type: {
                required: true
            },
            discount: {
                required: true
            },
            goods_arr:{
                required: true
            }
        },
        messages: {
            title: {
                required: icon+ "套餐名称不能为空！"
            },
            discount: {
                required: icon+ "套餐折扣率或满减金额不能为空！"
            },
            goods_arr:{
                required: icon+ "必须选择商品！"
            }
        }
    });
});